package com.wokeMy.view.channe.ronghui;

import android.app.Dialog;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.woke.MybankblindActivity;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.data.Data_bindcarlist;
import com.woke.data.UnionPayChannel;
import com.woke.method.MyApp;
import com.woke.serizedatas.Datas_load;
import com.wokeMy.view.base.BaseActivity;
import com.wokeMy.view.model.Constant;
import com.wokeMy.view.util.Util;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

public class RhuiRZhuActivity extends BaseActivity implements View.OnClickListener{
    ImageView back_rh_iv;
    RelativeLayout khcard_rh_rl;
    Button kait_ronhui_bt;
    TextView khk_rh_tv;
    EditText lhk_rh_et;

    Data_bindcarlist listdata;
    UnionPayChannel unionPayChannel;
    String pay_card_no,bankCode;
    String user_id;
    int way_id;
    String controller;
    int payWay;
    String useName,merchantName,merchantBillName,legalPersonID,merchantPersonPhone;

    Dialog dialog;
    @Override
    public void setContentView() {
        setContentView(R.layout.activity_ronghui);
    }

    @Override
    public void initActivity() {
        back_rh_iv.setOnClickListener(this);
        khcard_rh_rl.setOnClickListener(this);
        kait_ronhui_bt.setOnClickListener(this);
    }

    @Override
    public void initView() {
        back_rh_iv = myFindViewById(R.id.back_rh_iv);
        khcard_rh_rl = myFindViewById(R.id.khcard_rh_rl);
        khk_rh_tv = myFindViewById(R.id.khk_rh_tv);
        kait_ronhui_bt = myFindViewById(R.id.kait_ronhui_bt);
        lhk_rh_et = myFindViewById(R.id.lhk_rh_et);
    }

    @Override
    public void initData() {
        MyApp myApp=(MyApp)getApplication();
        Intent intent=getIntent();
        Datas_load datas_load=myApp.getDatas_load();
        Log.e("legalPersonName",datas_load.toString());
        user_id=datas_load.getId();
        unionPayChannel = (UnionPayChannel)(intent.getSerializableExtra("UnionPayChannel"));
        Log.e("unionPayChannel",unionPayChannel.toString());
        way_id=unionPayChannel.getWay();
        controller =unionPayChannel.getController();
        payWay=unionPayChannel.getKind();
        String name="名字为空";
        if (!datas_load.getUser_nicename().equals("")){
            name=datas_load.getUser_nicename();
        }else {
            name=datas_load.getCom_name();
        }
        useName=name;
        merchantName=name+"的商户";
        merchantBillName=name+"的商户";
        legalPersonID=datas_load.getUser_no();
        merchantPersonPhone=datas_load.getUser_login();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_rh_iv:
                finish();
                break;
            case R.id.khcard_rh_rl:
                Intent intent = new Intent (RhuiRZhuActivity.this, MybankblindActivity.class);
                intent.putExtra ("czhiortxian", "rhlz");
                startActivityForResult (intent, Constant.REQUEST_RHLZ);
                break;
            case R.id.kait_ronhui_bt:


                    bankCode=lhk_rh_et.getText().toString().trim();
                    if (pay_card_no==null||pay_card_no.equals("")){
                       Toast.makeText(this,"请选择结算卡",Toast.LENGTH_SHORT).show();
                    }else if(bankCode==null||bankCode.equals("")) {
                        Toast.makeText(this,"请填写联行号",Toast.LENGTH_SHORT).show();
                    }else{
                        if (!fastClick()) {
                            dialog = Util.createLoadingDialog(RhuiRZhuActivity.this);
                            rhlz();
                        }
                    }

                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==Constant.RESPONSE_RHLZ){
            listdata=(Data_bindcarlist)data.getSerializableExtra("listdata");
            pay_card_no=listdata.cardno;
            khk_rh_tv.setText(pay_card_no);
        }
    }
//    融汇入驻
    public void rhlz(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);
        params.put("controller",controller);
        params.put("payWay",payWay);
        params.put("merchantName",merchantName);
        params.put("merchantBillName",merchantBillName);
        params.put("installProvince","福建省");
        params.put("installCity","厦门市");
        params.put("installCounty","思明区");
        params.put("operateAddress","福建省厦门市思明区虎园路2号");
        params.put("merchantType","PERSON");
        params.put("legalPersonName",useName);
        params.put("legalPersonID",legalPersonID);
        params.put("merchantPersonName",useName);
        params.put("merchantPersonPhone",merchantPersonPhone);
        params.put("bankType","TOPRIVATE");
        params.put("accountName",useName);
        params.put("accountNo",pay_card_no);
        params.put("bankCode",bankCode);
//        params.put("creditCardNo","6225757545273378");

        Log.e(" 融汇入驻params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.RHSHLZ_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
             Util.closeDialog(dialog);
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("融汇入驻/////////", result);
                    try {
                        JSONObject  jsonObject =new JSONObject(result);
                        JSONObject jsonObject1=jsonObject.getJSONObject("data").getJSONObject("data");
                        String respcode=jsonObject1.getString("respcode");
                        if (respcode.equals("0000")){
                            Toast.makeText(RhuiRZhuActivity.this,"入驻成功",Toast.LENGTH_LONG).show();
                            finish();
                        }else if(respcode.equals("2015")){
                            Toast.makeText(RhuiRZhuActivity.this,"商户已经入驻过",Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(RhuiRZhuActivity.this,jsonObject1.getString("respmsg"),Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Util.closeDialog(dialog);
            }
        });
    }


}
